﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using APA_DAO;
using System.Data;
using System.Data.SqlClient;
using AMS;

namespace AMS_BATCH
{
    class Program
    {
        
        static void Main(string[] args)
        {
            DateTime dt = DateTime.Now;
            DateTime Firstday = dt.AddDays(-(int)dt.DayOfWeek);
            DateTime Endaday = Firstday.AddDays(6);

            ConfigDao dao= new ConfigDao();
            DataRow row = dao.getConfig();
            string mailBody = generateMailBody(Firstday, Endaday);
            string email = "";
            string pass ="";
            string user = "" ;
            if (row != null)
            {
                email = row["EMAIL"] == System.DBNull.Value ? "" : row["EMAIL"].ToString();
                pass = row["PASS"] == System.DBNull.Value ? "" : row["PASS"].ToString();
            }
            if (!string.IsNullOrEmpty(email))
            {
                int index = email.LastIndexOf("@");
                if (index > 0)
                {
                    user = email.Substring(0, index + 1);
                }
                if (!string.IsNullOrEmpty(pass))
                {
                    pass = SimpleEnrypt.Decrypt(pass, "AMS");
                    if (EmailManage.CheckForInternetConnection())
                    {
                        EmailManage.sendEmail(email, email, "[WEEKLY REPORT] Báo cáo tuần: " + Firstday.ToString("dd/MM/yyyy") + " ~ " + Endaday.ToString("dd/MM/yyyy"),
                            mailBody, user, pass);                            
                    }
                }
            }
        }

        private static string generateMailBody(DateTime fromDate, DateTime toDate)
        {
            StringBuilder strBuilder = new StringBuilder();
            int userId = 0;
            float totalOfRoom = 0;
            float totalOfPower = 0;
            float totalOfWater = 0;
            float totalOfOther = 0;
            float totalOfGetDeposit = 0;
            float totalOfRefundDeposit = 0;
            float totalOfPayChagre = 0;
            float totalPayment = 0;
            float totalGet = 0;
            float totalRefund = 0;
            strBuilder.Append("THỐNG KÊ TRONG TUẦN");
            strBuilder.Append("<br> Ngày đầu tuần (Chủ Nhật): " + fromDate.ToString("dd/MM/yyyy"));
            strBuilder.Append("<br> Ngày cuối tuần (Thứ 7): " + fromDate.ToString("dd/MM/yyyy"));
            ReportDao dao = new ReportDao();
            DataTable table = dao.getReportThuChiTheoNgay(fromDate, toDate, 0);
            foreach (DataRow row in table.Rows)
            {
                if (userId != int.Parse(row["NGUOI_THU"].ToString()))
                {
                    if (totalGet != 0 || totalRefund != 0)
                    {
                        strBuilder.Append("<br>");
                        strBuilder.Append("Tổng thu: " + totalGet.ToString("#,##0.##"));
                        strBuilder.Append("<br>Tổng chi: " + totalRefund.ToString("#,##0.##"));
                        strBuilder.Append("<br>Tổng doanh thu: " + (totalGet - totalRefund).ToString("#,##0.##"));
                        strBuilder.Append("<br>*************************************************************************************");
                    }
                    totalOfRoom = 0;
                    totalOfPower = 0;
                    totalOfWater = 0;
                    totalOfGetDeposit = 0;
                    totalOfRefundDeposit = 0;
                    totalPayment = 0;
                    totalGet = 0;
                    totalRefund = 0;
                    totalOfPayChagre = 0;
                    totalOfOther = 0;
                    userId = int.Parse(row["NGUOI_THU"].ToString());
                    strBuilder.Append("<br>");
                    strBuilder.Append("*************************************************************************************");
                    strBuilder.Append("<br>" + row["FULL_NAME"].ToString());
                }
                totalOfRoom += float.Parse(row["TIEN_PHONG"].ToString());
                totalOfPower += float.Parse(row["TIEN_DIEN"].ToString());
                totalOfWater += float.Parse(row["TIEN_NUOC"].ToString());
                totalOfOther += float.Parse(row["PHU_THU"].ToString());
                totalOfPayChagre += float.Parse(row["CHI_TIEU"].ToString());
                totalOfGetDeposit += float.Parse(row["THU_TIEN_COC"].ToString());
                totalOfRefundDeposit += float.Parse(row["TRA_TIEN_COC"].ToString());
                totalGet += (totalOfRoom + totalOfPower + totalOfWater + totalOfOther + totalOfGetDeposit);
                totalPayment += (totalOfPayChagre + totalOfRefundDeposit);
            }
            if (totalGet != 0 || totalRefund != 0)
            {
                strBuilder.Append("<br>");
                strBuilder.Append("Tổng thu: " + totalGet.ToString("#,##0.##"));
                strBuilder.Append("<br>Tổng chi: " + totalPayment.ToString("#,##0.##"));
                strBuilder.Append("<br>Tổng doanh thu: " + (totalGet - totalPayment).ToString("#,##0.##"));
                strBuilder.Append("<br>*************************************************************************************");
            }
            return strBuilder.ToString();
        }
    }
}
